package com.changpay.zywy.ui.activity.freeze

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.DaiLiAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.DaiLiBean
import com.changpay.zywy.bean.ProjectBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kotlinx.android.synthetic.main.activity_recycle.*
import kotlinx.android.synthetic.main.title_layout.*

class FreezePeopleActivity : BaseActivity(), IView {

    private var type: Int = 0
    private var positionData: ProjectBean? = null


    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_recycle
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "选择代理商名称"
        type = intent.getIntExtra("type",0)
        iv_back.setOnClickListener { finish() }
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val param = hashMapOf<String, String>()
        param["key"] = token

        when(type)
        {
            1->  // 冻结的冻结
            {
                param["type"] = "1"
                RetrofitHelper.service.get_freezeMb_list(param).sss(this) {
                    datas_two.addAll(it.result)
                    mAdapter_two.notifyDataSetChanged()
                }
            }

            2->  // 冻结的查看
            {
                param["type"] = "2"
                RetrofitHelper.service.get_freezeMb_list(param).sss(this) {
                    datas_two.addAll(it.result)
                    mAdapter_two.notifyDataSetChanged()
                }
            }
            3->  // 货款的查看
            {
                RetrofitHelper.service.get_mb_list(param).sss(this) {
                    datas_two.addAll(it.result)
                    mAdapter_two.notifyDataSetChanged()
                }
            }
            4->  // 权限查询 台均统计
            {
                RetrofitHelper.service.get_agent_list(param).sss(this) {
                    datas_two.addAll(it.result)
                    mAdapter_two.notifyDataSetChanged()
                }
            }
        }


    }


    /**
     * datas
     */
    private val datas_two = mutableListOf<DaiLiBean>()


    /**
     * Adapter
     */
    private val mAdapter_two: DaiLiAdapter by lazy {
        DaiLiAdapter(context, datas_two)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        val intent = Intent()
        intent.putExtra("gc_name", positionData?.gc_name)
        intent.putExtra("member_name", datas_two[position].member_name)
        intent.putExtra("member_phone", datas_two[position].member_mobile)
        intent.putExtra("member_id", datas_two[position].member_id.toString())
        intent.putExtra("gc_id", positionData?.gc_id.toString())
        setResult(200, intent)
        finish()
    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when (view.id) {
        }
    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter_two
        }
        mAdapter_two.run {
            bindToRecyclerView(recyclerView)
            onItemChildClickListener = this@FreezePeopleActivity.onItemChildClickListener
            onItemClickListener = this@FreezePeopleActivity.ItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }

    }

}